﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ScreenMonitor
{
    public class LocalImageProvider : AsycImageProviderBase
    {
        /// <summary>
        /// 获取或设置当前图片提供器的基础图片
        /// </summary>
        [Description("获取或设置当前图片提供器的基础图片")]
        public Image BindImage { get; set; }
        /// <summary>
        /// 图片等比缩放
        /// </summary>
        /// <param name="sourceImg">原图</param>
        /// <param name="targetWidth">指定的最大宽度</param>
        /// <param name="targetHeight">指定的最大高度</param>
        /// <returns>生成的新图</returns>
        private Image ZoomAuto(Image sourceImg, System.Double targetWidth, System.Double targetHeight)
        {
            //原图宽高均小于模版，不作处理，直接保存
            if (sourceImg.Width <= targetWidth && sourceImg.Height <= targetHeight)
            {
                return sourceImg;
            }
            else
            {
                //缩略图宽、高计算
                double newWidth = sourceImg.Width;
                double newHeight = sourceImg.Height;

                //宽大于高或宽等于高（横图或正方）
                if (sourceImg.Width > sourceImg.Height || sourceImg.Width == sourceImg.Height)
                {
                    //如果宽大于模版
                    if (sourceImg.Width > targetWidth)
                    {
                        //宽按模版，高按比例缩放
                        newWidth = targetWidth;
                        newHeight = sourceImg.Height * (targetWidth / sourceImg.Width);
                    }
                }
                //高大于宽（竖图）
                else
                {
                    //如果高大于模版
                    if (sourceImg.Height > targetHeight)
                    {
                        //高按模版，宽按比例缩放
                        newHeight = targetHeight;
                        newWidth = sourceImg.Width * (targetHeight / sourceImg.Height);
                    }
                }

                //生成新图
                //新建一个bmp图片
                System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
                //新建一个画板
                System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);

                //设置质量
                newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //置背景色
                newG.Clear(Color.White);
                //画图
                newG.DrawImage(sourceImg, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, sourceImg.Width, sourceImg.Height), System.Drawing.GraphicsUnit.Pixel);
                //释放资源
                newG.Dispose();
                return newImage;
            }
        }

        //public override System.Drawing.Image GetPreviewImage(System.Drawing.Size PreviewBoxSize)
        //{
        //    return ZoomAuto(BindImage, PreviewBoxSize.Width, PreviewBoxSize.Height);
        //}

        //public override System.Drawing.Image GetPartImage(System.Drawing.RectangleF ViewRect, System.Drawing.SizeF ImageSize)
        //{
        //    Bitmap bmp = new Bitmap((int)ImageSize.Width, (int)ImageSize.Height);
        //    Graphics g = Graphics.FromImage(bmp);
        //    g.DrawImage(BindImage, new Rectangle(0, 0, (int)ImageSize.Width, (int)ImageSize.Height), ViewRect, GraphicsUnit.Pixel);
        //    g.Dispose();
        //    return bmp;
        //}

        //public override System.Drawing.Size GetImagePhysicalSize()
        //{
        //    return BindImage.Size;
        //}

        public override void StartGetPartImage(RectangleF ViewRect, SizeF ImageSize)
        {
            if (BindImage == null)
                return;
            Bitmap bmp = new Bitmap((int)ImageSize.Width, (int)ImageSize.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.DrawImage(BindImage, new Rectangle(0, 0, (int)ImageSize.Width, (int)ImageSize.Height), ViewRect, GraphicsUnit.Pixel);
            g.Dispose();
            base.LastAsycCutRect = ViewRect;
            base.CutImage = bmp;
        }

        public override void StartGetPreviewImage(SizeF PreviewBoxSize)
        {
            if (BindImage == null)
                return;
            base.PreviewImage = ZoomAuto(BindImage, PreviewBoxSize.Width, PreviewBoxSize.Height);
        }

        public override void StartGetImagePhysicalSize()
        {
            if (BindImage == null)
                return;
            base.PhysicalSize = BindImage.Size;
        }

    }
}
